<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/layouts/taglib.jsp"%>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<title>选择模板</title>
</head>
<body>
<div class="page-container">
	<div class="cl pd-5 bg-1 bk-gray mt-20"> 
		<span class="l">
			<a href="javascript:;" onclick="select('${categoryId}',${type })" class="btn btn-success radius"><i class="Hui-iconfont">&#xe615;</i> 选择模板</a>
		</span> 
		<span class="r">共有数据：<strong id="totalCount">88</strong> 条</span> 
	</div>
	<div class="mt-20">
	<table class="table table-border table-bordered table-striped "  id="example">
		<thead>
			<tr class="text-c">
				<th width="25">请选择一个模板</th>
				<th width="40">模板名称</th>
				<th width="100">模板类型</th>
				<th width="90">创建时间</th>
			</tr>
		</thead>
		<tbody>
		</tbody>
	</table>
	</div>
</div>
<!--_footer 作为公共模版分离出去-->
<script type="text/javascript" src="${ctxLib }/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript" src="${ctxLib }/layer/2.4/layer.js"></script>
<script type="text/javascript" src="${ctxStatic }/h-ui/js/H-ui.min.js"></script> 
<script type="text/javascript" src="${ctxStatic }/h-ui.admin/js/H-ui.admin.js"></script>
<!--/_footer 作为公共模版分离出去-->

<!--请在下方写此页面业务相关的脚本-->
<script type="text/javascript" src="${ctxLib }/datatables/1.10.0/jquery.dataTables.min.js"></script> 
<script type="text/javascript" src="${ctxLib }/laypage/1.2/laypage.js"></script>
<script type="text/javascript">
	$(function(){
		$('.skin-minimal input').iCheck({
			checkboxClass: 'icheckbox-blue',
			radioClass: 'iradio-blue',
			increaseArea: '20%'
		});
		query();
	});
	
	function query(){
		var url = "${ctx }/template/list";
		$("#example").dataTable({
			"searching" : false,
			"bPaginate" : false,		//开启分页
			"bAutoWidth": true, 		//是否自适应宽度
			"bProcessing":true,		//是否显示“正在处理”这个提示信息
			"bSort":false,				//是否让各列具有按列排序功能
			"bStateSave":true,			//是否打开客户端状态记录功能
			"bDestroy":true,			//dataTable绑定时，将之前的那个数据对象清除掉，换以新的对象设置
			"serverSide":true,			//开启服务器处理模式
			"sAjaxSource":url,			//请求资源路径
			/*
			 使用ajax，在服务端处理数据
			 sSource:即是"sAjaxSource"
			 aoData:要传递到服务端的参数
			 fnCallback:处理返回数据的回调函数
			 */
			 "fnServerData": function(sSource, aoData, fnCallback){
				 var data = {"aodata" : JSON.stringify(aoData),"search_type":"${type}"};
				  $.ajax( { 
					  'type' : 'POST', 
					  "url": sSource, 
					  "dataType": "json", 
					  "data": data,
					  "success": function(resp) { 
						  	$("#totalCount").text(resp.iTotalRecords);
					   		fnCallback(resp);
					   } 
				  }); 
			 }, 
			 "columns": [
					        { "data": "id" },
					        { "data": "name" },
					        { "data": "type" },
					        { "data": "createDate" }
					    ],
					    "columnDefs": [{
					        targets: 0,
					        render: function(data, type, row, meta) {
					            return '<div class=\"skin-minimal\"><div class=\"radio-box\"><input  value=\"'+data+'\" type=\"radio\" \ name=\"radioItem\"  id=\"radio-'+data+'\"></div></div>';
					        }
					    },{
					    	targets:1,
					    	render:function(data,type,row,meta){
					    		var tip;
					        	var cid
					        	if(row.type == 1){
					        		cid = "${category.listTemplate}";
					        	}else{
					        		cid = "${category.contentTemplate}";
					        	}
					        	if(cid == row.id){
					        		tip = "<font color=\"red\">[已选择]</font>";
					        	}else{
					        		tip = "";
					        	}
					        	return data+tip;
					    	}
					    },{
					    	targets: 2,
					    	render: function(data, type, row, meta) {
					    		if(data == 1){
					    			return "列表页模板";
					    		}else{
					    			return "内容页模板";
					    		}
					        }
					    },{
					    	targets:3,
					    	render: function(data, type, row, meta) {
					    		if(data != null){
					    			return new Date(data).Format('yyyy-MM-dd hh:mm:ss');
					    		}else{
					    			return "";
					    		}
					        }
					    }],
					    "rowCallback": function (row, data, displayIndex) {//行定义
				     		 $(row).attr("class", "text-c");
						}
		});
	}
	
	function select(cid,type){
		if(cid == null || cid == '' || type == null){
			return;
		}
		var radio = $('input[name="radioItem"]:checked').val();
		if(radio == null || radio==''){
			layer.alert("您还未选择任何模板");
			return;
		}
		var formData = {id:cid};
		if(type == 1){
			formData.listTemplate = radio;
		}else{
			formData.contentTemplate = radio;
		}
		$.ajax({
			type: 'POST',
			url: '${ctx}/category/changeTemplate',
			contentType: 'application/json; charset=utf-8',
            data: JSON.stringify(formData),
            dataType: 'json',
			success: function(data){
				if(data.success){
					layer.alert('选择成功!',{icon:1},function(){
						var index = parent.layer.getFrameIndex(window.name);
						parent.location.href="${ctx }/category/list?id=${categoryId}";
                    	parent.layer.close(index);
					});
				}else{
					layer.msg(data.message,{icon:2});
				}
			},
			error:function(data) {
				console.log(data.msg);
			},
		});
	}
	</script> 
</body>
</html>